package com.zwz.service.impl;

import com.zwz.dto.TerminatedCaseDTO;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.RiskInfoMain;
import com.zwz.entity.TerminatedCase;
import com.zwz.mapper.RiskInfoMainMapper;
import com.zwz.mapper.TerminatedCaseMapper;
import com.zwz.service.TerminatedCaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TerminatedCaseServiceImpl implements TerminatedCaseService {

    @Autowired
    private RiskInfoMainMapper mainMapper;

    @Autowired
    private TerminatedCaseMapper caseMapper;

    @Override
    public TerminatedCaseDTO createWithMain(String  enterpriseId) {
        // 创建主表记录
        // 创建主表记录
        EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
        enterpriseInfo.setId(Long.valueOf(enterpriseId));

        // 设置子表关联
        TerminatedCase termination =new TerminatedCase();

        int key=0;
        termination.setId(key++);
        termination.setCaseNumber("123");

        //最后关联上
        termination.setEnterpriseId(Math.toIntExact(enterpriseInfo.getId()));

        // 业务校验
        validateTermination(termination);

        // 插入子表
        caseMapper.insert(termination);

        return new TerminatedCaseDTO(enterpriseInfo, termination);
    }

    private RiskInfoMain createMainRecord(Integer companyId) {
        RiskInfoMain main = new RiskInfoMain();
        main.setCompanyId(companyId);
        mainMapper.insert(main);
        return main;
    }

    private void validateTermination(TerminatedCase termination) {


        if (StringUtils.isBlank(termination.getCaseNumber())) {
            throw new IllegalArgumentException("案件编号不能为空");
        }
    }
}